package arithmetic;

public class BubbleSort {
    /**
     * 冒泡排序
     * 比较相邻元素，直到没有相邻元素可比较为止
     */
    public static void main(String[] args) {
        // 从小到大排序
        int[] arr1 = {2, 4, 3, 5, 6, 8, 23, 21, 34, 56, 0, 8};
        int len = arr1.length;
        int z = 0;
        for (int j = 0; j < len - 1; j++) {
            for (int i = 0; i < len - j - 1; i++) {
                if (arr1[i] > arr1[i + 1]) {
                    z = arr1[i + 1];
                    arr1[i + 1] = arr1[i];
                    arr1[i] = z;
                }
            }
        }

        //输出排序后的结果
        String sortResult = "";
        for (int i = 0; i < len; i++) {
            if (i == len - 1) {
                sortResult = sortResult + arr1[i];
            } else {
                sortResult = sortResult + arr1[i] + ",";
            }

        }
        System.out.println(sortResult);
    }
}
